#!/bin/bash


ZMACS=$0

# command
if [ -z ${1} ]; then
    COMMAND=edit
elif [ -e ${1} ]; then
    COMMAND=edit
    ARGS=${1}
else
    COMMAND=${1}
fi

# parse args
if [[ ${2} = "-s" ]]; then
    SESSION=${3}
    ARGS="${ARGS:-${@:4}}"
else
    SESSION=default
    ARGS="${ARGS:-${@:2}}"
fi

# do commands
case $COMMAND in
    help|-h|--help)
        cat << EOF
zmacs command [options] [argumnets]

commands supported:
  help - show this message
  list - show running emacs daemons
  start|stop|restart [-s session]
  edit|view [-s session] [emacs argumnets]

use "edit" by default: when there is no "command" specified
    or the first arg is a existing path
EOF
    ;;
    start)
        emacs --daemon=${SESSION}
        ;;
    stop)
        emacsclient -s ${SESSION} -e "(save-buffers-kill-emacs)"
        ;;
    kill)
        emacsclient -s ${SESSION} -e "(kill-emacs)"
        ;;
    restart)
        ${ZMACS} stop -s ${SESSION}
        ${ZMACS} start -s ${SESSION}
        ;;
    ls|list)
        ps aux | grep -i emacs | grep daemon | awk -F12 '{ print $NF }'
        ;;
    edit)
        emacsclient -c -n -s ${SESSION} ${ARGS}
        ;;
    view)
        echo "with bug"
        # emacsclient -c -n -s ${SESSION} -e "(setq buffer-read-only t)" ${ARGS}
        ;;
    *)
        echo "** unknown command ${COMMAND}"
        echo
        ${ZMACS} help
        ;;
esac
